import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import org.shoukaiseki.ddcs.actuator.conn.ConnectionFactory;
import org.shoukaiseki.ddcs.actuator.config.ConfigYaml;
import org.shoukaiseki.random.ChineseName;
import org.shoukaiseki.random.IdCardGenerator;

import java.io.*;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class YmlMain {

    static int count=0;

    public static void main(String[] args) throws Exception {
        System.out.println("hello");

        URL url = YmlMain.class.getClassLoader().getResource("ddcs.yml");
        ConfigYaml.loadYaml(new FileInputStream(url.getFile()));

        String string = ConfigYaml.getString("ddcs.datasource.names");
        System.out.println(string);

        final Connection conn = ConnectionFactory.createNonSharedConn("test1");


        final long starttime = System.currentTimeMillis();
//        insert1(conn);
        insert2(conn);

        conn.close();
        long endtime = System.currentTimeMillis();
        long time = endtime - starttime;
        System.out.println((time/1000)+"秒"+(time%1000));
        //46秒,43秒,45.59
    }

    public static void insert2(final Connection conn) throws Exception {

        conn.setAutoCommit(false);
        String sql = "SET UNIQUE_CHECKS=0";
        Statement st = conn.createStatement();
        st.execute(sql);

        final HanyuPinyinOutputFormat format= new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);

        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();


        final PreparedStatement ps = conn.prepareStatement("insert testdb1.user_1(userid,name) values(?,?)");

        for (int j = 0; j < 100; j++) {
                cachedThreadPool.execute(new Runnable() {
                    public void run() {
                        try {
                            for (int i = 0; i < 1000; i++) {
                                final String chineseName = ChineseName.randomChineseName();
                                final StringBuffer enname=new StringBuffer();
                                enname.append( PinyinHelper.toHanYuPinyinString(chineseName,format,"",true));
                                String generate = IdCardGenerator.generate();
                                enname.append(generate.substring(14));
                                ps.setString(1,enname.toString());
                                ps.setString(2,chineseName);
                                ps.addBatch();
                                synchronized (this){
                                    count++;
                                    if(count%2000==0){

                                        ps.executeBatch();
                                        conn.commit();
                                    }
                                }
                            }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                    }
                });
        }
        cachedThreadPool.shutdown();
        while (true) {
            if (cachedThreadPool.isTerminated()) {
                break;
            }
        }
        ps.executeBatch();
        conn.commit();
    }


    public static void insert1(Connection conn) throws Exception {

        final HanyuPinyinOutputFormat format= new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        final Statement statement = conn.createStatement();
        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < 1000; i++) {

                Thread thread=new Thread(){
                    @Override
                    public void run() {
                        try {
                            final String chineseName = ChineseName.randomChineseName();
                            final StringBuffer enname=new StringBuffer();
                            enname.append( PinyinHelper.toHanYuPinyinString(chineseName,format,"",true));
                            String generate = IdCardGenerator.generate();
                            enname.append(generate.substring(14));
                            statement.executeUpdate("insert testdb1.user_1(userid,name) values('"+enname+"','"+chineseName+"')");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                thread.run();
            }
        }
    }


}
